Ln (Unix)
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
top
ln is a shell command for creating a link file to an existing file or directory.cite-ref-unixkp-1-0[1] By default, the command creates a hard link, but with the -s command line option, it creates a symbolic link.cite-ref-2[2] Most systems disallow a hard link to a directory since such links could disrupt the structure of a file system and interfere with the operation of other utilities.cite-ref-3[3] The command can create a symbolic link to non-existent file.cite-ref-gnulnman-4-0[4]
Contents
• Links
• Use
• Options
• Examples
• See also
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Links
A link allows more than one path to refer to the same file.
A hard link is a directory entry that refers to a file's inode (an internal reference). A file can have multiple hard links – each referring to the same inode.cite-ref-libc-9-0[9] Creating a hard link does not copy the contents of the file; but merely causes another name to be associated with the same content. When a hard link is created for a file, a counter in its inode is incremented, and removing a hard link results in decrementing the counter. A file is not deleted (to allow reusing its storage space) until its reference count reaches zero.cite-ref-unixkp-1-1[1]cite-ref-ibminode-10-0[10] A hard link can only refer to an object on the same file system.cite-ref-11[11]
A symbolic link refers to another file by its path (a symbolic reference).cite-ref-gnulnman-4-1[4] When encountered during pathname resolution, modify the pathname resolution to be taken to the location which the symbolic link contains. The content of a symbolic link is the target path, which can also be examined via the readlink command.cite-ref-12[12] A symbolic link may specify a path that does not refer to an existing file. Resolving such a symbolic link will fail until a file is created at its specified path. Also, a symbolic link to an existing file will fail to resolve if the file is moved.cite-ref-libc-9-1[9]
Use
Per the Single Unix Specification, the ln command is specified in the Shell and Utilities (XCU) document.cite-ref-susoverview-13-0[13] POSIX includes a similar specification.cite-ref-sus-5-1[5]
If neither target file nor target directory are specified, links are created in the current working directory.
Options
The specification indicates command line options that must be supported:
• -f (force) Clobbers an existing file if the output path specifies an existing file
• -L If a source operand specifies a symbolic link, create a hard link to the link's target file
• -P If a source operand specifies a symbolic link, create a hard link to the symbolic link file
• -s Create symbolic links instead of hard links; causes -L and -P to be silently ignored
If more than one of the mutually-exclusive options -L and -P is specified, the last option specified determines the resulting behavior.
If the -s option is not specified and neither a -L nor a -P option is specified, the implementation defines which of the -L and -P options is used by default.
Single file invocation
The command creates a new link to the path indicated by source; stored at the path indicated by target. The syntax is as follows:
ln [-fs] [-L|-P] source target
Multiple file invocation
The command creates a new link to each file specified by the source_# operands; stored in an existing directory indicated by target_dir.
ln [-fs] [-L|-P] source_1 source_2 ... target_dir
Examples
The following creates a hard link to file foo called bar.
ln foo bar
The following creates a symbolic link to file foo called bar.
ln -s foo bar
See also
References
cite-note-22. ↑ "ln - The Open Group Base Specifications Issue 7". pubs.opengroup.org. The IEEE and The Open Group. Retrieved 7 August 2015.
cite-note-33. ↑ "Why are hard links not allowed to directories in UNIX/Linux?". unix.stackexchange.com. Retrieved 7 August 2015.
cite-note-gnulnman-44. ↑ "GNU Coreutils: ln invocation". gnu.org. Free Software Foundation, Inc. Retrieved 7 August 2015.
cite-note-sus-55. ↑ ln – Shell and Utilities Reference, The Single UNIX Specification, Version 5 from The Open Group
cite-note-77. ↑ "Native Win32 ports of some GNU utilities". unxutils.sourceforge.net.
cite-note-libc-99. ↑ citerefloosemorestallmanmcgrathoramLoosemore, Sandra; Stallman, Richard M.; McGrath, Roland; Oram, Andrew; Drepper, Ulrich. The GNU C Library Reference Manual (PDF). Free Software Foundation, Inc. p. 392. Retrieved 7 August 2015.
cite-note-ibminode-1010. ↑ "Speaking UNIX: It is all about the inode". ibm.com. IBM developerWorks. Retrieved 7 August 2015.
cite-note-1212. ↑ readlinkman"readlink(1) - Linux man page". linux.die.net. Retrieved 7 August 2015.
cite-note-susoverview-1313. ↑ "The Single UNIX Specification, Version 4 - Overview". unix.org. unix.org. Retrieved 7 August 2015.
External links
The Wikibook
Guide to Unix
has a page on the topic of:
File System Utilities - ln